package com.android.systemui.ios.wall;

import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.android.systemui.ios.R;

public class IpSetWallpaperLayout extends FrameLayout implements View.OnClickListener {
    private static final String TAG = IpSetWallpaperLayout.class.getSimpleName();
    private ImageView mImageWallpaper;
    private ImageView mThumbHome;
    private ImageView mThumbLock;
    private View mBtnSetAll;
    private View mBtnSetHome;
    private String mWallpaperPath;
    private Bitmap mWallpaperBitmap;
    private String mThumbPath;
    private int mType = 1;
    private Runnable mOnSetRunnable;
    private boolean isSyncSetLockWall = false;
    private View mSetLayout;
    private View mBtnCancel;
    private View mBtnDone;
    private SettingViewItemCallback mDoneCallback;

    public IpSetWallpaperLayout(Context context) {
        super(context);
    }

    public IpSetWallpaperLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IpSetWallpaperLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mBtnCancel = findViewById(R.id.btn_set_wallpaper_cancel);
        mBtnCancel.setOnClickListener(this);
        mBtnDone = findViewById(R.id.btn_set_wallpaper_done);
        mBtnDone.setOnClickListener(this);
        mSetLayout = findViewById(R.id.set_wallpaper_done_layout);

        mImageWallpaper = (ImageView) findViewById(android.R.id.icon);
        mThumbHome = (ImageView) findViewById(android.R.id.icon1);
        mThumbLock = (ImageView) findViewById(android.R.id.icon2);

        mBtnSetAll = findViewById(R.id.btn_set_wallpaper_pair);
        mBtnSetAll.setOnClickListener(this);

        mBtnSetHome = findViewById(R.id.btn_set_wallpaper_home);
        mBtnSetHome.setOnClickListener(this);
    }

    public void setPrepareSetWallpaper(int type, String thumb, String path) {
        mWallpaperPath = path;
        mThumbPath = thumb;
        mType = type;
        mWallpaperBitmap = BitmapFactory.decodeFile(path);
        Bitmap tbmp = BitmapFactory.decodeFile(thumb);
        mThumbHome.setImageBitmap(tbmp);
        mThumbLock.setImageBitmap(tbmp);
        mImageWallpaper.setImageBitmap(mWallpaperBitmap);
        mBtnSetAll.setEnabled(true);
        mBtnSetHome.setEnabled(true);
    }

    private void setHomeScrWallpaper(Bitmap bitmap) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    WallpaperManager wpm = (WallpaperManager) getContext().getSystemService(Context.WALLPAPER_SERVICE);
                    if (isSyncSetLockWall) {
                        wpm.setBitmap(mWallpaperBitmap, null, true, WallpaperManager.FLAG_LOCK);
                    }
                    wpm.setBitmap(mWallpaperBitmap, null, true, WallpaperManager.FLAG_SYSTEM);
                    /*ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    mWallpaperBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                    byte[] wallpaper_byte = baos.toByteArray();
                    File sd = Environment.getExternalStorageDirectory();
                    String path = sd.getPath() + "/HomeWall";
                    File filedir = new File(path);
                    if (!filedir.exists()) {
                        filedir.mkdir();
                    }

                    File sdFile = new File(path, "Home_wallpaper_file");
                    try {
                        FileOutputStream file = new FileOutputStream(sdFile);
                        file.write(wallpaper_byte);
                        file.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }*/
                } catch (Exception ignored) {
                }
            }
        }).start();
    }

    public void setSystemLockWallpaper(Bitmap bmp) {
        try {
            //mWallpaperBitmap = bmp;
            WallpaperManager wpm = (WallpaperManager) getContext().getSystemService(Context.WALLPAPER_SERVICE);
            wpm.setBitmap(bmp, null, true, WallpaperManager.FLAG_LOCK);
            wpm.setBitmap(bmp, null, true, WallpaperManager.FLAG_SYSTEM);
        } catch (Exception ignored) {
        }
    }

    @Override
    public void onClick(View v) {
        final int vID = v.getId();
        if (vID == R.id.btn_set_wallpaper_cancel) {
            post(mCloseRunnable);
            if (mDoneCallback != null)
                mDoneCallback.setMode(2);
        } else if (vID == R.id.btn_set_wallpaper_done) {
            mSetLayout.setVisibility(View.VISIBLE);
            mBtnCancel.setVisibility(View.GONE);
            mBtnDone.setVisibility(View.GONE);
            return;
        } else if (vID == R.id.btn_set_wallpaper_pair) {
            v.setEnabled(false);
            isSyncSetLockWall = true;
            ItemSetting it = new ItemSetting(-1, mType, mWallpaperPath);//mThumbPath
            if (mDoneCallback != null)
                mDoneCallback.onDoneEditItem(it);
            //Log.d(TAG, "[onClick] btn_set_wallpaper_pair mWallpaperPath = "+mWallpaperPath);
            //setSystemLockWallpaper(mWallpaperPath);
            //setHomeScrWallpaper(mWallpaperBitmap);
            //if (mOnSetRunnable != null) {
            //    postDelayed(mOnSetRunnable, 300);
            //}
        } else if (vID == R.id.btn_set_wallpaper_home) {
            v.setEnabled(false);
            findViewById(R.id.set_wallpaper_done_layout).setVisibility(View.GONE);
            isSyncSetLockWall = false;
            ItemSetting it = new ItemSetting(-1, mType, mWallpaperPath);
            if (mDoneCallback != null)
                mDoneCallback.onCustomEditItem(it, true);
            post(mCloseRunnable);
            //setHomeScrWallpaper(mWallpaperBitmap);
            //if (mOnSetRunnable != null) {
            //    postDelayed(mOnSetRunnable, 300);
            //}
            //show Edit home layout

        }

        if (mDoneCallback != null)
            mDoneCallback.userActivity();
        mSetLayout.setVisibility(View.GONE);
    }

    public void setOnSetRunnable(Runnable runnable) {
        mOnSetRunnable = runnable;
    }

    public void entryScreen() {
        setAlpha(0.0f);
        setVisibility(GONE);
    }

    private final Runnable mCloseRunnable = new Runnable() {

        @Override
        public void run() {
            animate().alpha(0.0f).withEndAction(new Runnable() {

                @Override
                public void run() {
                    setVisibility(GONE);
                }
            }).start();
        }
    };

    public void setOnDoneCallback(SettingViewItemCallback cb) {
        mDoneCallback = cb;
    }

}



